package com.neusoft.resturant.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.resturant.common.utils.PageResult;
import com.neusoft.resturant.common.utils.PageUtils;
import com.neusoft.resturant.common.utils.R;
import com.neusoft.resturant.entity.ResAddress;
import com.neusoft.resturant.entity.ResDishes;
import com.neusoft.resturant.service.IResAddressService;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 收货地址Controller
 * 
 * @author lzx
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/restaurant/address")
public class ResAddressController extends BaseController
{
    @Autowired
    private IResAddressService resAddressService;

    /**
     * 查询收货地址列表
     */

    @GetMapping("/list")
    public PageResult list(ResAddress resAddress, Integer pageNum, Integer pageSize)
    {
        if(pageNum==null){
            pageNum=1;
        }
        if(pageSize==null){
            pageSize=8;

        }

        PageHelper.startPage(pageNum, pageSize);
        List<ResAddress> list = resAddressService.selectResAddressList(resAddress);

        if (list.size()==0){
            return null;
        }
        //4、对查询的结果进行封装
        PageInfo<ResAddress> thumbupPageInfo2 = new PageInfo<>(list);
        PageResult pageResult2 = PageUtils.getPageResult(thumbupPageInfo2);

        return pageResult2;
    }



    /**
     * 获取收货地址详细信息
     */

    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id)
    {

        return R.success( resAddressService.selectResAddressById(id));
    }

    /**
     * 新增收货地址
     */


    @PostMapping
    public R add(@RequestBody ResAddress resAddress)
    {
        resAddress.setUser_id(this.getUserId());
        resAddressService.insertResAddress(resAddress);
        return R.success("新增成功");
    }

    /**
     * 修改收货地址
     */

    @PutMapping
    public R edit(@RequestBody ResAddress resAddress)
    {
        resAddress.setUser_id(this.getUserId());
        resAddressService.updateResAddress(resAddress);
        return R.success("修改成功");
    }

    /**
     * 删除收货地址
     */

	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {
        int i = resAddressService.deleteResAddressByIds(ids);
        return R.success("删除成功"+i+"条");
    }
}
